package com.kyzh.core.adapters.providers

import android.view.View
import android.widget.TextView
import com.chad.library.adapter.base.entity.node.BaseNode
import com.chad.library.adapter.base.provider.BaseNodeProvider
import com.chad.library.adapter.base.viewholder.BaseViewHolder
import com.gushenge.core.beans.Small
import com.gushenge.core.dao.GlobalKeys
import com.gushenge.core.requests.UserRequest
import com.kyzh.core.R
import com.kyzh.core.pager.weal.accountbuy.SmallToSellActivity
import com.kyzh.core.uis.alert
import com.kyzh.core.utils.startActivity
import com.gushenge.core.toast


class SmallProvider() :BaseNodeProvider(){
    override val itemViewType: Int
        get() = 0

    override val layoutId: Int
        get() = R.layout.small_child_item
    override fun convert(helper: BaseViewHolder, item: BaseNode) {
        val small = item as Small
        helper.setText(R.id.tvName, small.user_name)
                .setText(R.id.tvTime, small.reg_time)
        helper.getView<TextView>(R.id.btDelete).setOnClickListener {
            context.alert(context.getString(R.string.deleteSmall3), String.format(context.getString(R.string.deleteSmall4),small.data.size), context.getString(R.string.sure), context.getString(R.string.cancel),{
                UserRequest.smallDel(small.uid){
                    val pos = helper.adapterPosition
                    getAdapter()?.data?.removeAt(pos)
                    getAdapter()?.notifyDataSetChanged()
                }
            },{}) .setCancelable(false)

        }
        helper.getView<TextView>(R.id.btSold).setOnClickListener {
            if (small.data.isEmpty()) {
                context.getString(R.string.smallNoPlayGame).toast()
            } else {
                context.startActivity<SmallToSellActivity>(GlobalKeys.KEY to small)
            }
        }
    }

    override fun onClick(helper: BaseViewHolder, view: View, data: BaseNode, position: Int) {
        if ((data as Small).data.isNullOrEmpty()) {
            context.getString(R.string.smallNoPlayGame).toast()
        } else {
            getAdapter()?.expandOrCollapse(position)
        }
    }
}